package com.jsyl.lwbk.business.service.impl;

import com.jsyl.lwbk.business.domain.CourseChildProject;
import com.jsyl.lwbk.business.mapper.ChildProjectConfigMapper;
import com.jsyl.lwbk.business.service.CourseChildProjectService;
import com.jsyl.lwbk.business.constant.ProjectCodeConstant;
import com.jsyl.lwbk.business.domain.ChildProjectConfig;
import com.jsyl.lwbk.business.mapper.CourseChildProjectMapper;
import com.jsyl.lwbk.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Fitch
 * 2022/3/28
 */
@Service
public class CourseChildProjectServiceImpl implements CourseChildProjectService {

    @Autowired
    private CourseChildProjectMapper courseChildProjectMapper;
    @Autowired
    private ChildProjectConfigMapper childProjectConfigMapper;

    @Override
    public Map saveProject(CourseChildProject project) {
        Map result=new HashMap();
        result.put("linkRuleType",0);
        if(project !=null && !project.getChildId().equals(0L)){
            //单个小孩备课
            //判断该环节是否是自选环节，自选环节返回该项目已训练次数
            Integer linkType = this.courseChildProjectMapper.getLinkRuleType(project.getChildCourseId(),project.getLinkNo());
            result.put("linkRuleType",linkType);
            if(linkType.equals(1)){
                ChildProjectConfig config = this.childProjectConfigMapper.selectOne(project.getChildId(),project.getProjectCode());
                if(config!=null){
                    result.put("completeTimes",config.getCompleteTimes());
                }
            }
            //项目1项目2（大滑板）项目联动，即只需要选择一次
            CourseChildProject local = this.courseChildProjectMapper.selectById(project.getId());
            if(local !=null && local.getInitFour().equals(local.getInitFive())){
                if(StringUtils.isNotEmpty(project.getLinkFour())){
                    project.setLinkFive(project.getLinkFour());
                }
                if(StringUtils.isNotEmpty(project.getLinkFive())){
                    project.setLinkFour(project.getLinkFive());
                }
            }

            this.courseChildProjectMapper.saveProject(project);
            //其他环节初始化项目相同时 同步保存

            return result;
        }else {
            //全部学员备课
            this.courseChildProjectMapper.saveProjectByCourseId(project);
            return result;
        }
    }

    @Override
    public int saveAllLink(CourseChildProject project) {
        //项目1项目2（大滑板）项目联动，即只需要选择一次
        if(project.getInitProject().equals(ProjectCodeConstant.大滑板类)){
            if(StringUtils.isNotEmpty(project.getLinkFour())){
                project.setLinkFive(project.getLinkFour());
            }
            if(StringUtils.isNotEmpty(project.getLinkFive())){
                project.setLinkFour(project.getLinkFive());
            }
        }
        return this.courseChildProjectMapper.saveAllLink(project);
    }

    @Override
    public int saveTrainAmount(CourseChildProject project) {
        return this.courseChildProjectMapper.saveTrainAmount(project);
    }

    @Override
    public CourseChildProject getProject(Long childCourseId, Long childId) {
        return this.courseChildProjectMapper.selectOne(childCourseId,childId);
    }
}
